package com.wx.task.future.v2;

import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * pao cha fc
 *  用两个线程 T1 和 T2 来完成烧水泡茶程序，  T3
 *  * T1 负责洗水壶、烧开水、这三道工序，
 *  * T2 负责洗茶壶、洗茶杯、拿茶叶三道工序，
 *  * 其中 T3 等待 T1  T2 完成拿茶叶的工序 完成 泡茶
 *
 *  使用 CompletableFuture 来完成
 * @author wang.xun
 * @date 2023/08/03
 */
@Slf4j
public class PaoChaFc {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Void> cf1 = CompletableFuture.runAsync(() -> {

            log.info("{} 开始洗茶壶",Thread.currentThread().getName());
            ThreadUtil.sleep(1000);
            log.info("{} 烧开水",Thread.currentThread().getName());
            ThreadUtil.sleep(15000);

        });

        CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> {

            log.info("{} 洗茶具", Thread.currentThread().getName());
            ThreadUtil.sleep(1000);
            log.info("{} 洗茶杯", Thread.currentThread().getName());
            ThreadUtil.sleep(2000);
            log.info("{} 拿茶杯", Thread.currentThread().getName());
            ThreadUtil.sleep(2000);
            return "龙井";
        });

        CompletableFuture<String> cf3 = cf1.thenCombine(cf2, (__, chaye) -> {
            log.info("{} 开始泡茶 茶叶是：【{}】", Thread.currentThread().getName(), chaye);
            return "开始喝茶吧 " + chaye;
        });

        String rs = cf3.get();
        log.info(rs);

    }
}
